package app.server.project.cloud.random.controller;

import app.server.project.cloud.random.dto.device.*;
import app.server.project.core.constants.Def;
import app.server.project.core.http.RespModel;
import app.server.project.main.service.dto.device.*;
import app.server.project.cloud.random.service.DevMgrService;
import app.server.project.cloud.random.service.UserDeviceService;
import cn.hutool.core.util.IdUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户设备管理
 */
@AllArgsConstructor
@RequestMapping("/user/dev")
@RestController
public class UserDevController {

    private UserDeviceService userDeviceService;
    private DevMgrService devMgrService;
    /**
     * 拉取用户设备列表
     * @param userId
     * @return
     */
    @PostMapping("/getAllDevice")
    public RespModel<List<DeviceDto>> getAllDevice(@RequestHeader(Def.UserId) long userId){
        return RespModel.success(this.userDeviceService.getAllDevice(userId));
    }

    /**
     * 绑定设备
     * @param userId
     * @param deviceId
     */
    @PostMapping("/bindDevice/{deviceId}")
    public RespModel<Void> bindDevice(@RequestHeader(Def.UserId) long userId,@PathVariable long deviceId){
        this.userDeviceService.bindDevice(userId, deviceId);
        return RespModel.success();
    }


    /**
     * 解绑设备
     * @param userId
     * @param deviceId
     * @return
     */
    @PostMapping("/unBind/{deviceId}")
    public RespModel<Void> unBind(@RequestHeader(Def.UserId) long userId,@PathVariable long deviceId){
        this.userDeviceService.unBind(userId, deviceId);
        return RespModel.success();
    }

    /**
     * 改变房间
     * @param req
     * @param userId
     */
    @PostMapping("/changeRoomId")
    public RespModel<Void> changeRoomId(@RequestBody ChangeDevRoomReq req, @RequestHeader(Def.UserId)  long userId){
        this.userDeviceService.changeRoomId(req, userId);
        return RespModel.success();
    }

    /**
     * 改变设备名称
     * @param req
     * @param userId
     */
    @PostMapping("/updateName")
    public RespModel<Void> updateName(@RequestBody ChangeNameReq req, @RequestHeader(Def.UserId) long userId){
        this.userDeviceService.updateName(req, userId);
        return RespModel.success();
    }

    /**
     * 设置支路名称
     * @param req
     * @return
     */
    @PostMapping("/updateDevChName")
    public RespModel<Void> updateDevChName(@RequestBody ChangeDevChNameReq req){
        this.userDeviceService.updateDevChName(req);
        return RespModel.success();
    }

    /**
     * app同步设备状态
     * @param devStatusMgr
     */
    @PostMapping("/appSyn")
    public RespModel<Void> appSyn(@RequestBody DevStatusMgr devStatusMgr){
        this.devMgrService.appSyn(devStatusMgr);
        return RespModel.success();
    }

    /**
     * 设备同步状态信息
     * @param devStatusMgr
     */
    @PostMapping("/devSyn")
    public RespModel<Void> devSyn(@RequestBody DevStatusMgr devStatusMgr){
        this.devMgrService.devSyn(devStatusMgr);
        return RespModel.success();
    }

}
